package mars.activity;

import cn.com.RotationHelper;
import bear.download.Circlenaread;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewAnimator;
import android.widget.ViewFlipper;

public class TestFilp extends Activity implements OnGestureListener,OnClickListener {
    /** Called when the activity is first created. */
	private ViewFlipper flipper;//ViewFlipper实例   
	private GestureDetector detector;//触摸监听实例
	private Button return0;
	private String yeshu="";
	public LinearLayout layout1;  	  
    String tag;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ye);
        return0=(Button)findViewById(R.id.return0);
        //return0.setOnClickListener(new return0Listener());
        return0.setOnClickListener(this);
        detector = new GestureDetector(this);//初始化触摸探测
        		flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);//获得ViewFlipper实例
        		String sendnames[] = Circlenaread.sendname1.split("\\$");
        		String createtimes[]=Circlenaread.createtime1.split("\\$");
        		String pagecontents[]=Circlenaread.pagecontent1.split("\\$");
        		 for(int i=0;i<Circlenaread.n;i++){
        			 yeshu=(i+1)+"";
        		((ViewGroup) flipper).addView(addTextView(yeshu+"."+sendnames[i]+":"+pagecontents[i]+"————"+createtimes[i]));//将View添加到flipper队列中
        		}
        		 layout1 = (LinearLayout) findViewById(R.id.layout1);  
        	        showView();       		 
        }
    private View addTextView(String text) {
    	TextView tv = new TextView(this);
		tv.setText(text);
		tv.setGravity(Gravity.CENTER);
		tv.setTextColor(Color.BLACK);
		tv.setPadding(45, 30, 45, 30);
		tv.setTextSize(20);
		return tv;
	}
@Override  
    public boolean onTouchEvent(MotionEvent event) {   
        return this.detector.onTouchEvent(event);   
  }        
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
		if (e1.getX() - e2.getX() > 120) {//如果是从右向左滑动
						//注册flipper的进出效果
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
			((ViewAnimator) this.flipper).showNext();
			return true;
		} else if (e1.getX() - e2.getX() < -120) {//如果是从左向右滑动
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out));
			((ViewAnimator) this.flipper).showPrevious();
			return true;
		}
		return false;
	}
public boolean onDown(MotionEvent e) {
	// TODO Auto-generated method stub
	return false;
}
public void onLongPress(MotionEvent e) {
	// TODO Auto-generated method stub
	
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
		float distanceY) {
	// TODO Auto-generated method stub
	return false;
}
public void onShowPress(MotionEvent e) {
	// TODO Auto-generated method stub
	
}
public boolean onSingleTapUp(MotionEvent e) {
	// TODO Auto-generated method stub
	return false;
}  
public void showView() {  
    /* 取得Intent中的Bundle对象 */  
    Bundle bundle = this.getIntent().getExtras();  

    if (bundle != null) {  
        /* 取得Bundle对象中的数据 */  
        tag = bundle.getString("TestFlip");  
        System.out.println("tag =" + tag);  
        if (tag.equals("TestFlip")) {  
            rotateHelper = new RotationHelper(this,  
                    2);  
            rotateHelper.applyLastRotation(layout1, -90, 0);  
        }  
    }  
}  
    RotationHelper rotateHelper;   
    public void onClick(View v) {  
        // TODO Auto-generated method stub  
        rotateHelper = new RotationHelper(this, 1);  
        rotateHelper.applyFirstRotation(layout1, 0, -90);  
    }
    public void jumpToSecond() {  
        Intent in = new Intent();  
        in.setClass(this, User.class);  
        // new一个Bundle对象，并将要传递的数据传入  
        Bundle bundle = new Bundle();  
        bundle.putString("front", "First");  
        /* 将Bundle对象assign给Intent */  
        in.putExtras(bundle);  
        // 如果已经打开过的实例，将不会重新打开新的Activity  
        startActivity(in);
        finish();
    }
}
